Ext.onReady(function(){
	Ext.QuickTips.init();//要表单验证错误后提示错误信息，  需加上这行
	Ext.form.Field.prototype.msgTarget = 'side';
	//加上上面那行代码，有错误时会在输入框侧面显示一个感叹号，鼠标移上去会显示错误信息，反之是鼠标移到到输入框就显示错误信息
	//自动引入js文件     ps.要判断文件是否重复引入
	var includeObj=function(obj){
		var script=document.getElementsByTagName(script);
        var script = document.createElement("SCRIPT");
        script.setAttribute("type","text/javascript");
        script.setAttribute("src","./Js/"+obj+".js");
        var head = document.head;
        head.appendChild(script);
	};
	
	var loader=new Ext.tree.TreeLoader({
		url:Fn.url("Nav/tree?cmd=list")
	});
	
	var root=new Ext.tree.AsyncTreeNode({  
		id:"root", 
		text:"根节点",  
		loader:loader,
		expanded: true
	});
	
	//添加节点窗口
	var nodeWin=new Ext.Window({
		closeAction:'hide',
		width:300,
		height:170,
		modal:true,
		title:"添加节点",
		buttons:[{
			text:"添加",
			handler:function(){
				var treepanel=Ext.getCmp("App_Js_main_tree_list");
				var form=Ext.getCmp("App_Js_main_tree_addNode_form");
				var rec=treepanel.getSelectionModel(); 
				var selectNode=rec.selNode.attributes;
				if(selectNode){
					var selectId=selectNode.nav_id;//得到节点nav_id
					form.getForm().submit({//submit通过name属性进行传值
						url: Fn.url('Nav/tree?cmd=addNode'),
						params:{
							selectId:selectId
		          		},
						success:function(response){
	          	        	var response=Ext.util.JSON.decode(response.responseText);//Ext.decode该方法也可以
	          	        	if(response.success==false){
	          	        		Ext.Msg.alert("提示","同一级别不能重复节点名");
	          	        	} else {
	          	        		Ext.Msg.alert("提示","添加成功!!!");	          	        		
	          	        		nodeWin.hide();
	          	        		treepanel.getLoader().load();
	          	        	}
	          	        }
					})
				}
			}
		}],
		items:[{
			xtype:'form',
			id:"App_Js_main_tree_addNode_form",
			padding:10,
			items:[{
				xtype:"textfield",
				name:"title",
				fieldLabel:"节点名称",
				anchor:"95%"
			}]
		}]
	});
    
	function checkTime(i)
	{
		if (i<10) 
		  {i="0" + i}
		  return i
	}
	//主面板
	var viewport=new Ext.Viewport({		
		layout:'border',
		listeners: {
		     afterrender: function(t){
		    	//var session_value =document.getElementById('userName11111').getAttribute("value");
		    	//alert(session_value);
		    	var today=new Date();
				var h=today.getHours();
				var m=today.getMinutes();
				var s=today.getSeconds();
				m=checkTime(m);
				s=checkTime(s);
				var aaa=document.getElementById("AddProduct-SelectedDir");
				aaa.innerHTML="&nbsp;&nbsp;登陆的时间为："+h+":"+m+":"+s
				var session_value =document.getElementById('sessionName').getAttribute("value");
				var name=document.getElementById('session');
				if(session_value =="admin"){
					name.innerHTML="【欢迎管理员：】<font color='red'>"+session_value+"</font>";
					return;
				}
				name.innerHTML="【欢迎会员：】<font color='red'>"+session_value+"</font>";
		     }
		},
		items: [
		    {
				region:"north",    
				title:'留言板管理',
				padding:"1",
				tbar:['->',{
					html:'<div id="session">欢迎你：</div>'
				},'','',{
					html:'<div id="AddProduct-SelectedDir">当前时间</div>'						
				},'-',{
					text:"退出",
					handler:function(){
						window.location.href="../../../messageTp/index.php";
					}
				}]
		    },
		    {
				xtype:'treepanel',
				id:"App_Js_main_tree_list",
				title:'导航菜单',
				width:250,
				region:"west",
				collapsible:true,	
				root:root,
				tbar:['->',{
					text:"添加节点",
					handler:function(){
						nodeWin.show();
					}
				}],
				listeners:{
					click:function(node,rowIndex){	
						var value=node.attributes.value;
						var id=node.attributes.id;
						var centerPanel=Ext.getCmp("index_center_region"); 
						includeObj(value);						
						setTimeout(function(){
							// Fn.log(1);
							var panel=Ext.getCmp(id);
							if(typeof panel==="undefined"){
								var obj=null;
								eval('obj=new '+value+'()');//eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码。								
								// alert(panel);
								// Fn.log(2);
								centerPanel.add(obj.panel);
								obj.panel.show(); 
							}else{
								 panel.show();
							}
						},20);
					},
					dblclick:function(t){
						
					}
				},
	            rootVisible : false
		    },
		    {
		    	xtype:"panel",
		    	region:"center",
		    	padding:"1",
		    	layout:"fit",
		    	items:[{
		    		xtype:'tabpanel',
				 	id:"index_center_region",
				 	items:[{
				 		title:'默认'	,
				 		html:"留言板"
				 	}]
		        }]
		   }
		]
	});		
})